<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/3/24 0024
 * Time: 16:06
 * 有来视频
 */
defined('BASEPATH') OR exit('No direct script access allowed');
include_once('./simple_html_dom.php');

class Youlai extends CI_Controller
{
    /**
     * 获取科室对应疾病id
     */
    public function ids(){
        exit;
        $url = "https://www.youlai.cn/video/pcplayup/1.html";
        $content = geturl($url);
        $html = str_get_html($content);
        foreach($html->find('dd',2)->find('a') as $ak=>$a)
        {
            $dept = $a->href;
            $dept_id = str_replace(['/video/pcplayup/','.html'],'',$dept);
            $dept_name = trim($a->innertext);
            $sql = "";
            if($ak > 0){
                $url = "https://www.youlai.cn/video/pcplayup/".$dept_id.".html";
                $content = geturl($url);
                $html = str_get_html($content);
            }
            // 通过科室id获取疾病列表
            foreach($html->find('dd',3)->find('a') as $aak => $aa){
                if($aak == 0){
                    continue;
                }
                $dis = $aa->href;
                $dis_id = str_replace(['/video/pcplayup/'.$dept_id.'/','.html'],'',$dis);
                $dis_name = trim($aa->innertext);
                $sql .= "(".$dept_id.",'".$dept_name."',".$dis_id.",'".$dis_name."'),";
            }
            $sql = substr($sql,0,-1);
            $sql = 'INSERT ignore INTO ai_youlai_ids(dept_id,dept_name,dis_id,dis_name) VALUES '.$sql;
            //echo $sql;exit;
            $this->db->query($sql);
        }
        //dd($div);
    }

    /**
     * 获取详情列表
     */
    public function urls($did = 2038){
        if(!$did){
            echo "error";
            exit;
        }
        $ids = $this->db->where('id',$did)->get('youlai_ids')->row_array();
        if(!$ids){
            echo "nodata";
            exit;
        }
        for($i=1;$i<=10;$i++){ // 最多十页
            $list_html = "https://www.youlai.cn/video/pcplayup/".$ids['dept_id']."/".$ids['dis_id']."/1/".$i.".html";
            //echo $list_html."<br>";
            $content = geturl($list_html);
            $html = str_get_html($content);
            $lis = $html->find("ul[id=videoList] li");
            $sql = "";
            foreach($lis as $li){
                $did = $ids['id'];
                $dept = $ids['dept_name'];
                $dis = $ids['dis_name'];
                $title = trim($li->find("p",0)->find("a",0)->innertext);
                $url = trim($li->find("p",0)->find("a",0)->href);
                $sql .= "(".$did.",'".$dept."','".$dis."','".$title."','".$url."'),";
            }
            $sql = substr($sql,0,-1);
            $sql = 'INSERT ignore INTO ai_youlai_url(did,dept,dis,title,url) VALUES '.$sql;
            //echo $sql;exit;
            $this->db->query($sql);
            if(count($lis) < 20){ // 不够20的话，肯定没有下一页了
                break;
            }
        }
        echo $did + 1;
    }

    public function click_num(){
        // 查看没有click_num 的数据，进行修改
        $youlai = $this->db->query("select * from ai_spider_youlai_disease_voice where url_id>0 and click_num=0 order by url_id asc limit 1")->row_array();
        if(!$youlai){
            echo "nodata";
            exit;
        }
        $url = $this->db->where('id',$youlai['url_id'])->get('youlai_voice_url')->row_array();
        $arc_id = str_replace(["/ask/",".html"],"",$url['url']);
        $data = "info=".urlencode('[{"a_id":"'.$arc_id.'","a_type":3}]');
        //echo $data;exit;
        $res = http_post("https://www.youlai.cn/get/tongji",$data);
        $res_info = json_decode($res,true);
        if(!is_array($res_info)){
            echo "error" ; // 跳下一个
            sleep(3);
        }else{
            $click_num = $res_info['data']['list'][0]['a_views'];
            $up['click_num'] = $click_num;
            $this->db->where('id',$youlai['id'])->update('ai_spider_youlai_disease_voice',$up);
            echo $youlai['id'];
        }
    }

    public function url_id(){
        $res = $this->db->query("select * from ai_spider_youlai_disease_video where url_id is NULL")->result_array();
        foreach($res as $vo){
            // 通过视频路径获取url_id
            $info = str_replace(['youlai_video/','.mp4'],'',$vo['video_url_local']);
            $url_id = substr($info,-4);
            $up['url_id'] = $url_id;
            $this->db->where('id',$vo['id'])->update('spider_youlai_disease_video',$up);
            echo $url_id."<br>";
            //exit;
        }
    }

    public function in_list(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        // 查询所有url_id 不匹配的数据
        $normal = $this->db->query("select vid from ai_spider_39_disease_video group by vid")->result_array();
        $nor_ids = array_column($normal,'vid');
        $tmp = $this->db->query("select vid from ai_39_disease_video")->result_array();
        $tmp_ids = array_column($tmp,'vid');
        $diff = array_diff($tmp_ids,$nor_ids);
        //dd($diff);
        //$diff = array_slice($diff,12498);
        $vids = implode(",",$diff);
        $list = $this->db->query("select id from ai_39_disease_video where vid in(".$vids.")")->result_array();
        foreach($list as $vo){
            $redis->rPush('jiankang_video_list',$vo['id']);
        }

    }

    public function list_move(){
        $redis = new Redis();
        $redis->connect('82.157.157.146');
        $redis->auth('spring');
        for($i=1;$i<=10000;$i++){
            $url_id = $redis->lPop('youlai_list_tmp');
            $redis->rPush('youlai_list',$url_id);
        }
        echo "ok";
    }



}